package com.iplant.basic.serviceImpl.dao.mss;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.mss.MSSMaterialGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class MSSMaterialGroupDAO extends BaseDAO {

	private static MSSMaterialGroupDAO Instance = null;
	private static Logger logger = LoggerFactory.getLogger(MSSMaterialGroupDAO.class);

	private MSSMaterialGroupDAO() {
		super();

		setTableName("mss_materialgroup");
	}

	public static MSSMaterialGroupDAO getInstance() {
		if (Instance == null)
			Instance = new MSSMaterialGroupDAO();
		return Instance;
	}

	public void MSS_UpdateMaterialGroup(BMSEmployee wLoginUser, MSSMaterialGroup wMaterialGroup,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);

		try {
			if (wMaterialGroup == null || StringUtils.isEmpty(wMaterialGroup.Name)) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}
			MSSMaterialGroup wMaterialGroupO = this.MSS_CheckMaterialGroup(wLoginUser, wMaterialGroup, wErrorCode);
			if (wMaterialGroupO.ID > 0) {
				wErrorCode.set(ExceptionTypes.Duplication.getValue());
			}
			if (wErrorCode.Result != 0)
				return;

			wMaterialGroup.GroupFullName = "";
			if (wMaterialGroup.ParentGroupID > 0) {
				wMaterialGroupO = this.MSS_QueryMaterialGroup(wLoginUser, wMaterialGroup.ParentGroupID, "", wErrorCode);
				if (wErrorCode.Result != 0)
					return;

				if (wMaterialGroupO == null || wMaterialGroupO.ID <= 0) {
					wMaterialGroup.ParentGroupID = 0;

				} else {

					wMaterialGroup.GroupFullName = StringUtils.Format("{0}{1}/", wMaterialGroupO.GroupFullName,
							wMaterialGroupO.Name);
				}
			}

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("Name", wMaterialGroup.Name);
			wParams.put("Code", wMaterialGroup.Code);
			wParams.put("ParentGroupID", wMaterialGroup.ParentGroupID);
			wParams.put("GroupFullName", wMaterialGroup.GroupFullName);
			wParams.put("Description", wMaterialGroup.Description);

			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wMaterialGroup.Active);

			if (wMaterialGroup.ID > 0) {
				wParams.put("ID", wMaterialGroup.ID);
				super.Update(super.getTableName("mss_materialgroup"), "ID", wParams);
			} else {
				wParams.put("CreatorID", wLoginUser.getID());
				wParams.put("CreateTime", Calendar.getInstance());
				wMaterialGroup.ID = super.Insert(super.getTableName("mss_materialgroup"), wParams);
			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_UpdateMaterialGroup", ex);
		}
	}

	public void MSS_ActiveMaterialGroup(BMSEmployee wLoginUser, MSSMaterialGroup wMaterialGroup, int wActive,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);
		// 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

		try {
			if (wMaterialGroup == null || wMaterialGroup.ID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("ID", wMaterialGroup.ID);
			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wActive);
			super.Update(super.getTableName("mss_materialgroup"), "ID", wParams);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());

			logger.error("MSS_ActiveMaterialGroup", ex);
		}
	}

	public List<MSSMaterialGroup> MSS_QueryMaterialGroupList(BMSEmployee wLoginUser, String wName, int wParentGroupID,
			String wGroupFullName, int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
		return this.MSS_QueryMaterialGroupList(wLoginUser, -1, "", wName, wParentGroupID, wGroupFullName, wActive,
				wPagination, wErrorCode);
	}

	private List<MSSMaterialGroup> MSS_QueryMaterialGroupList(BMSEmployee wLoginUser, int wID, String wCode,
			String wName, int wParentGroupID, String wGroupFullName, int wActive, Pagination wPagination,
			OutResult<Integer> wErrorCode) {
		List<MSSMaterialGroup> wMSSMaterialGroupList = new ArrayList<MSSMaterialGroup>();
		wErrorCode.set(0);

		try {

			// Step02:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = "";
			wSQLText = StringUtils.Format(
					"Select t.*,t3.Name as CreatorName ,t4.Name as EditorName, t2.Name as ParentGroupName,"
							+ " t2.Code as ParentGroupCode,COUNT(t5.ID) as ItemCount,COUNT(t6.ID) as MaterialCount from {0}.mss_materialgroup t  "
							+ " left join {0}.mss_materialgroup t2 on t.ParentGroupID=t2.ID "
							+ " left join {0}.mss_materialgroup t5 on t5.ParentGroupID=t.ID "
							+ " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
							+ " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
							+ " left join {0}.mss_material t6 on t6.MaterialGroupID=t.ID "
							+ " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t.Code=:Code)"
							+ " AND (:ParentGroupID<0 or  t.ParentGroupID=:ParentGroupID)  AND (:Active<0 or  t.Active=:Active) "
							+ " AND (:Name = '''' or  t.Name like :Name) AND (:GroupFullName = '''' or  t.GroupFullName like :Name) group by t.ID ",
					super.getDBName());

			wSQLText = this.DMLChange(wSQLText);

			wParams.put("ID", wID);
			wParams.put("Code", wCode);
			wParams.put("ParentGroupID", wParentGroupID);
			wParams.put("Active", wActive);
			wParams.put("Name", StringUtils.isEmpty(wName) ? "" : ("%" + wName + "%"));
			wParams.put("GroupFullName", StringUtils.isEmpty(wGroupFullName) ? "" : (wGroupFullName + "%"));

			wMSSMaterialGroupList = super.QueryForList(wSQLText, wParams, wPagination, MSSMaterialGroup.class);

			// Step03:配置系统
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_QueryMaterialGroupList", ex);
		}
		return wMSSMaterialGroupList;
	}

	public MSSMaterialGroup MSS_QueryMaterialGroup(BMSEmployee wLoginUser, int wID, String wCode,
			OutResult<Integer> wErrorCode) {
		MSSMaterialGroup wResult = new MSSMaterialGroup();
		wErrorCode.set(0);

		try {
			List<MSSMaterialGroup> wList = null;
			if (wID > 0) {
				wList = this.MSS_QueryMaterialGroupList(wLoginUser, wID, "", "", -1, "", -1, Pagination.getDefault(),
						wErrorCode);

			} else if (StringUtils.isNotEmpty(wCode)) {
				wList = this.MSS_QueryMaterialGroupList(wLoginUser, -1, wCode, "", -1, "", -1, Pagination.getDefault(),
						wErrorCode);
			}
			if (wList != null && wList.size() > 0) {
				wResult = wList.get(0);
			}
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_QueryMaterialGroupByID", ex);
		}
		return wResult;
	}

	public MSSMaterialGroup MSS_CheckMaterialGroup(BMSEmployee wLoginUser, MSSMaterialGroup wMSSMaterialGroup,
			OutResult<Integer> wErrorCode) {
		MSSMaterialGroup wMaterialGroup = new MSSMaterialGroup();
		wErrorCode.set(0);

		try {

			// Step0:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = StringUtils.Format("Select * from {0}.mss_materialgroup "
					+ " where ID!=:ID  AND ( (Name =:Name and ParentGroupID=:ParentGroupID) or (:Code != '''' and Code=:Code)) ",
					super.getDBName());
			wParams.clear();
			wParams.put("ID", wMSSMaterialGroup.ID);
			wParams.put("Name", wMSSMaterialGroup.Name);
			wParams.put("ParentGroupID", wMSSMaterialGroup.ParentGroupID);
			wParams.put("Code", wMSSMaterialGroup.Code);

			wSQLText = this.DMLChange(wSQLText);
			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams);
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wMaterialGroup.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wMaterialGroup.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
				wMaterialGroup.Code = StringUtils.parseString(wSqlDataReader.get("Code"));
				wMaterialGroup.GroupFullName = StringUtils.parseString(wSqlDataReader.get("GroupFullName"));

				wMaterialGroup.ParentGroupID = StringUtils.parseInt(wSqlDataReader.get("ParentGroupID"));
				wMaterialGroup.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wMaterialGroup.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wMaterialGroup.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wMaterialGroup.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wMaterialGroup.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_CheckMaterialGroup", ex);
		}
		return wMaterialGroup;
	}

}
